var app = {
    online: 0,
    uuid: '', // 0bcb314df5d03f51
    counter_number: '',
    counter_number_title: '',
    terminal_number: '',
    terminal_number_title: '',
    loop: 120000,
    host: '',
    initialize: function () {
        var online = parseInt(localStorage.getItem('online'));
        var uuid = parseInt(localStorage.getItem('uuid'));
        var m_loop = parseInt(localStorage.getItem('loop'));

        var m_host = localStorage.getItem('host');

        this.counter_number = localStorage.getItem('counter_number');
        this.counter_number_title = localStorage.getItem('counter_number_title');
        this.terminal_number = localStorage.getItem('terminal_number');
        this.terminal_number_title = localStorage.getItem('terminal_number_title');


        this.online = parseInt(online);
        this.loop = m_loop ? m_loop : 120000;

        this.host = m_host ? m_host : ''; // https://www.spdbh5.com/checkout/api.php

        $('#uuid').val(this.uuid);
        $('#loop').val(this.loop);

        $('#counter_number').val(this.counter_number);
        $('#terminal_number').val(this.terminal_number);

        $('#host').val(this.host);
        if (!this.host) {
            $(".modal.set-modal").show();
        }

        var _this = this;
        if (!this.intervalId) {
            this.intervalId = setInterval(function () {
                try {
                    _this.update();
                } catch (e) {
                }
            }, this.loop);
        }

        setTimeout(function () {
            _this.check();
        }, 2000);
    },
    /**
     * 设置参数
     * @param uuid
     * @param counter_number
     * @param terminal_number
     */
    setConfig: function (uuid, counter_number, terminal_number) {
        this.uuid = uuid;
        this.counter_number = counter_number;
        this.terminal_number = terminal_number;

        var counter_number_title = $('#counter_number_select').find('option[value="' + counter_number + '"]').html();
        var terminal_number_title = $('#terminal_number_select').find('option[value="' + terminal_number + '"]').html();
        localStorage.setItem('counter_number', counter_number);
        if (counter_number_title) {
            localStorage.setItem('counter_number_title', counter_number_title);
            this.counter_number_title = counter_number_title;
        }
        localStorage.setItem('terminal_number', terminal_number);
        if (terminal_number_title) {
            localStorage.setItem('terminal_number_title', terminal_number_title);
            this.terminal_number_title = terminal_number_title;
        }
    },
    /**
     *
     * @param loop
     * @param host
     */
    setParam: function (loop, host) {
        localStorage.setItem('loop', loop);
        localStorage.setItem('host', host);

        if (this.loop != loop) {
            this.loop = loop;
            if (this.intervalId) {
                clearInterval(this.intervalId);
            }

            var _this = this;
            this.intervalId = setInterval(function () {
                try {
                    _this.update();
                } catch (e) {
                }
            }, this.loop);
        }


        this.host = host;
    },
    check: function () {
        var _this = this;
        var uuid = $('#uuid').val();
        if (_this.host) {
            $.get(_this.host, {
                op: 'check',
                uuid: uuid
            }, function (ret) {
                if (typeof ret == 'string') {
                    ret = JSON.parse(ret);
                }

                if (ret.code == 0) {
                    _this.online = 1; // 已绑定
                    _this.setConfig(ret.data.uuid, ret.data.counter_number, ret.data.terminal_number);
                    $('#uuid').val(ret.data.uuid);
                    $('#counter_number').val(ret.data.counter_number);
                    $('#terminal_number').val(ret.data.terminal_number);

                    $("#counter_number_select").val(ret.data.counter_number);
                    $("#terminal_number_select").val(ret.data.terminal_number);
                    _this.update();
                } else {
                    _this.online = 0;// 未绑定
                    $('#counter_number').val('');
                    $('#terminal_number').val('');
                }

                $.get(_this.host, {
                    op: 'get',
                    uuid: uuid
                }, function (ret) {
                    if (typeof ret == 'string') {
                        ret = JSON.parse(ret);
                    }

                    _this.select = ret.data;
                    if (_this.select) {
                        _this.select.push({
                            "counter_number": "" + _this.counter_number,
                            "counter_name": _this.counter_number_title ? _this.counter_number_title : _this.counter_number,
                            "terminal_number": "" + _this.terminal_number,
                            "terminal_Name": _this.terminal_number_title ? _this.terminal_number_title : _this.terminal_number
                        });
                    } else {
                        _this.select = [{
                            "counter_number": "" + _this.counter_number,
                            "counter_name": _this.counter_number_title ? _this.counter_number_title : _this.counter_number,
                            "terminal_number": "" + _this.terminal_number,
                            "terminal_Name": _this.terminal_number_title ? _this.terminal_number_title : _this.terminal_number
                        }];
                    }

                    $("#counter_number_select").empty();
                    var counter_list = [];
                    $.each(_this.select, function (i, o) {
                        if (typeof counter_list[o.counter_number] == 'undefined') {
                            counter_list[o.counter_number] = o.counter_name;
                            $("#counter_number_select").append('<option value="' + o.counter_number + '">' + o.counter_name + '</option>');
                        }
                    })

                    $("#counter_number_select").val($('#counter_number').val());
                    $("#counter_number_select").unbind().change(function () {
                        $("#terminal_number_select").empty();
                        var terminal_list = [];

                        $.each(_this.select, function (i, o) {

                            if ($("#counter_number_select").val() == o.counter_number) {
                                if (typeof terminal_list[o.terminal_number] == 'undefined') {
                                    counter_list[o.terminal_number] = o.terminal_Name;
                                    $("#terminal_number_select").append('<option value="' + o.terminal_number + '">' + o.terminal_Name + '</option>');
                                }
                            }
                        })

                        $("#terminal_number_select").val($('#terminal_number').val());
                    })

                    $("#counter_number_select").change();
                });
            })
        }
    },
    bind: function (uuid, counter_number, terminal_number, loop, host) {
        this.setParam(loop, host);
        var _this = this;
        if (this.host) {
            $.get(this.host, {
                op: 'bind',
                uuid: uuid,
                counter_number: counter_number,
                terminal_number: terminal_number
            }, function (ret) {
                if (typeof ret == 'string') {
                    ret = JSON.parse(ret);
                }

                if (ret.code == 0) {
                    _this.online = 1; // 已绑定
                    _this.setConfig(uuid, counter_number, terminal_number);
                    _this.update();
                } else {
                    _this.online = 0;// 未绑定
                    alert(ret.msg);
                }
                console.log(ret);
            })
        } else {
        }
    },

    update: function () {
        var _this = this;
        if (this.host) {
            $.get(this.host, {
                op: 'goods',
                uuid: _this.uuid,
                counter_number: _this.counter_number,
                terminal_number: _this.terminal_number
            }, function (ret) {
                if (typeof ret == 'string') {
                    ret = JSON.parse(ret);
                }

                if (ret.code == 0) {
                    _this.online = 1; // 已绑定
                    _this.display(ret.data);
                } else {
                    _this.online = 0;// 未绑定
                    alert(ret.msg);
                }
            })
        } else {
        }
    },

    display: function (data) {
        $('.mid-swiper').find('.swiper-wrapper').empty();
        // var len = data.length;
        // var page = Math.ceil(len / 9.0);

        var html = '';

        var index = 0;
        $.each(data, function (i, o) {

            if (index % 9 == 0) {
                if (index > 0) {
                    html += '</div></div>';
                }
                html += '<div class="swiper-slide"><div class="center">';
            }
            var node = '<div class="col-sm-4" cost="' + o.price + '"  goods_id="' + o.id + '">' +
                '<div class="img-wrap">' +
                '<img src="' + o.img + '"/>' +
                '</div>' +
                '<div class="product-title">' +
                o.title +
                '<br/>' +
                '<small>￥</small>' +
                '<strong>' + o.price + '</strong>' +
                '</div>' +
                '</div>';
            html += node;
            index++;
        })

        if (index % 9 != 0) {
            html += '</div></div>';
        }

        $('.mid-swiper').find('.swiper-wrapper').append(html);

        mid.update();
        mid.onResize();

        fun_click_goods();
    },

    pay: function (key, pay_type) {
        var _this = this;
        if (this.host) {
            var goods_json = JSON.stringify(goods); // goods_json == '{}' ? 1 : 2,
            var goods_list = [];
            if (goods_json == '{}') {
                goods_list.push('-1:1:' + price);
                count = 1;
            } else {
                $.each(goods, function (i, o) {
                    goods_list.push(i + ':' + o.count + ':' + o.price);
                });
            }
            if (debug) {
                alert(JSON.stringify({
                    op: 'pay',
                    uuid: _this.uuid,
                    counter_number: _this.counter_number,
                    terminal_number: _this.terminal_number,
                    pay_type: pay_type,
                    price: price,
                    count: count,
                    key: key,
                    goods: '{' + goods_list.join(',') + '}'
                }));
            }
            $.ajax({
                url: this.host,
                type: 'get',
                data: {
                    op: 'pay',
                    uuid: _this.uuid,
                    counter_number: _this.counter_number,
                    terminal_number: _this.terminal_number,
                    pay_type: pay_type,
                    price: price,
                    count: count,
                    key: key,
                    goods: '{' + goods_list.join(',') + '}'
                },
                success: function (ret) {
                    if (typeof ret == 'string') {
                        ret = JSON.parse(ret);
                    }

                    if (ret.code == 0) {
                        var msg = '支付成功';
                        if (pay_type == 1) {
                            msg += '<br>卡余额：';
                            msg += ret.msg;
                        }
                        jQuery('.alert .modal-content').html(msg);
                        jQuery('.alert').show();
                        jQuery('.alert').click(function () {
                            jQuery(this).hide();
                        });
                        check.slideTo(1, 100, false);
                        setTimeout("jQuery('.alert').hide();", 2000);
                        price = '0';
                        count = '0';
                        goods = {};
                        jQuery(".count span").html("X " + count + "份");
                        jQuery(".cost span").html("￥" + price + ".00");
                        jQuery(".half .right .strong").html("￥" + price + ".00");
                        jQuery(".half .right .small span").html(count);
                        jQuery('#custom-price').html('');
                        check.slideTo(1, 100, false);
                        window.tools.stopScan();
                        // 退出支付中
                        padding = false;
                    } else {
                        jQuery('.alert .modal-content').html(ret.msg);
                        jQuery('.alert').show();
                        jQuery('.alert').click(function () {
                            jQuery(this).hide();
                        });
                        setTimeout("jQuery('.alert').hide();", 2000);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        } else {
        }
    }
};

app.initialize();